package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.ProjectDetailsActivity;
import com.wnhz.work.yz.bean.ProjectChildBean;
import com.wnhz.work.yz.view.RoundImageView;

import org.xutils.x;

import java.util.List;

/**
 * Created by admin on 2016/12/27.
 */
public class ProjectContentAdapter extends BaseAdapter {
    private Context context;
    private List<ProjectChildBean> dataList;
    private String budget_id;
    private String reName, canPay;

    public ProjectContentAdapter(Context context, List<ProjectChildBean> dataList, String budget_id, String canPay, String reName) {
        this.budget_id = budget_id;
        this.context = context;
        this.dataList = dataList;
        this.canPay = canPay;
        this.reName = reName;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ProjectContentViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ProjectContentViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.project_content_item, null);
            viewHolder.iv_img = (RoundImageView) convertView.findViewById(R.id.iv_img);
            viewHolder.tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name);
            viewHolder.tv_product = (TextView) convertView.findViewById(R.id.tv_product);
            viewHolder.tv_goods_info = (TextView) convertView.findViewById(R.id.tv_goods_info);
            viewHolder.tv_goods_type = (TextView) convertView.findViewById(R.id.tv_goods_type);
            viewHolder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            viewHolder.tv_price2 = (TextView) convertView.findViewById(R.id.tv_price2);
            viewHolder.tv_goods_number = (TextView) convertView.findViewById(R.id.tv_goods_number);
            viewHolder.ll_max_item = (LinearLayout) convertView.findViewById(R.id.ll_max_item);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ProjectContentViewHolder) convertView.getTag();
        }

        x.image().bind(viewHolder.iv_img, dataList.get(position).getGoods_pic());
        viewHolder.tv_goods_name.setText(dataList.get(position).getGoods_name());
//        if (!"整体浴室".equals(dataList.get(position).getPackage_cate())) {
//            viewHolder.tv_product.setBackground(context.getResources().getDrawable(R.drawable.button_ju));
//        } else {
//            viewHolder.tv_product.setBackground(context.getResources().getDrawable(R.drawable.button_main));
//        }
        if ("1".equals(dataList.get(position).getTypeid())) {
            viewHolder.tv_product.setBackground(context.getResources().getDrawable(R.drawable.button_ju));
        } else if ("0".equals(dataList.get(position).getTypeid())) {
            viewHolder.tv_product.setBackground(context.getResources().getDrawable(R.drawable.button_main));
        } else if ("2".equals(dataList.get(position).getTypeid())) {
            viewHolder.tv_product.setBackground(context.getResources().getDrawable(R.drawable.buttom_green));
        }
        viewHolder.tv_product.setText(dataList.get(position).getPackage_cate());
        viewHolder.tv_goods_info.setText(dataList.get(position).getSku_info());
        viewHolder.tv_goods_type.setText("房间号:" + dataList.get(position).getRoom_number());
        if ("1".equals(dataList.get(position).getIf_tepi())) {
            String a = "<s>" + "¥ " + dataList.get(position).getPrice() + "</s>";
            viewHolder.tv_price2.setText(Html.fromHtml(a));
            viewHolder.tv_price2.setVisibility(View.VISIBLE);
            viewHolder.tv_price2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            viewHolder.tv_price.setText("¥ " + dataList.get(position).getTepi_price());
        } else {
            viewHolder.tv_price.setText("¥ " + dataList.get(position).getPrice());
        }

        viewHolder.tv_price2.setText("¥ " + dataList.get(position).getPrice());
        viewHolder.tv_goods_number.setText("x" + dataList.get(position).getNums());

        if ("999".equals(canPay)) {
            viewHolder.ll_max_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
        } else
            viewHolder.ll_max_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, ProjectDetailsActivity.class);
                    try {
                        intent.putExtra("budget_id", budget_id);
                        intent.putExtra("tv_project_code", reName + "");
                        intent.putExtra("canPay", canPay + "");

                        context.startActivity(intent);
                    } catch (Exception e) {
                        notifyDataSetChanged();
                    }
                }
            });
        return convertView;
    }

    class ProjectContentViewHolder {
        RoundImageView iv_img;
        TextView tv_goods_name;
        TextView tv_product;
        TextView tv_goods_info;
        TextView tv_goods_type;
        TextView tv_price, tv_price2;
        TextView tv_goods_number;
        LinearLayout ll_max_item;
    }
}
